package com.cloud.magic_api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.domain.Result;
import com.cloud.common.magic_init.database.DataSourcePoolFactory;
import com.cloud.common.magic_init.database.enums.PoolMation;
import com.cloud.magic_api.entity.SysDatabase;
import com.cloud.magic_api.mapper.SysDatabaseMapper;
import com.cloud.magic_api.service.ISysDatabaseService;
import com.cloud.utils.StringUtils;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;

import javax.sql.DataSource;
import java.util.List;

/**
 * 数据库配置信息
 *
 * @author yzj
 * @since 2022-08-25
 */
@Service
public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDatabase> implements ISysDatabaseService {

    @Resource
    private SysDatabaseMapper sysDatabaseMapper;

    @Autowired
    private MagicDynamicDataSource dynamicDataSource;

    @PostConstruct
    public void init() {
        SysDatabase sysDatabase = new SysDatabase();
        List<SysDatabase> databaseList = sysDatabaseMapper.selectSysDatabaseList(sysDatabase);
        if (StringUtils.isNotNull(databaseList)) {
            // 动态配置数据源信息
            databaseList.forEach(database -> {
                DataSource newDataSource = DataSourcePoolFactory
                        .create(PoolMation.getPoolClassByType(database.getPoolType())).wrap(database);
                dynamicDataSource.add(database.getDataName(), newDataSource);
            });
        }
    }

    /**
     * 数据库配置信息分页
     *
     * @author yzj
     */
    @Override
    public List<SysDatabase> selectSysDatabaseList(SysDatabase sysDatabase) {
        return sysDatabaseMapper.selectSysDatabaseList(sysDatabase);
    }

    /**
     * 数据库配置信息新增
     *
     * @author yzj
     */
    @Override
    public Result insertObject(SysDatabase sysDatabase) {
        int rows = sysDatabaseMapper.insert(sysDatabase);
        if (rows > 0) {
            DataSource newDataSource = DataSourcePoolFactory
                    .create(PoolMation.getPoolClassByType(sysDatabase.getPoolType())).wrap(sysDatabase);
            dynamicDataSource.add(sysDatabase.getDataName(), newDataSource);
        }
        return Result.success("操作成功");
    }

    /**
     * 数据库配置信息修改
     *
     * @author yzj
     */
    @Override
    public Result updateObject(SysDatabase sysDatabase) {
        int rows = sysDatabaseMapper.updateById(sysDatabase);
        if (rows > 0) {
            // 移除数据源信息
            if (dynamicDataSource.delete(sysDatabase.getDataName())) {
                DataSource newDataSource = DataSourcePoolFactory
                        .create(PoolMation.getPoolClassByType(sysDatabase.getPoolType())).wrap(sysDatabase);
                dynamicDataSource.add(sysDatabase.getDataName(), newDataSource);
            }
        }
        return Result.success("操作成功");
    }

    /**
     * 刷新数据源信息
     *
     * @param dataName
     * @return
     */
    @Override
    public Result refreshDatabase(String dataName) {
        SysDatabase sysDatabase = sysDatabaseMapper.selectDatabaseByName(dataName);
        if (StringUtils.isNotNull(sysDatabase)) {
            DataSource newDataSource = DataSourcePoolFactory
                    .create(PoolMation.getPoolClassByType(sysDatabase.getPoolType())).wrap(sysDatabase);
            dynamicDataSource.add(sysDatabase.getDataName(), newDataSource);
            return Result.success("操作成功");
        } else {
            return Result.error("操作失败:数据源信息不存在");
        }
    }

}
